'use strict';

const db = uniCloud.database()
exports.main = async (event, context) => {
	let { relateUuid, wordBankId } = event;
	try {
		// 查询用户某个词库的背单词状态
		const res = await db.collection('memorization').where({
			relateUuid,
			wordBankId,
		}).get();
		
		if(res.data.length === 0){
		    let addRes = await db.collection("memorization").add({
				relateUuid,
				wordBankId,
				todayStudyNum: 0,
				todayNeedReviewNum: 40,
				studyedWords: []
			});
			return {
				code: 200,
				message: '增加数据成功',
				data: addRes
			}
		}
		
		return {
		  code: 200,
		  message: '背单词状态查询成功',
		  data: res.data[0]
		};
	} catch(err) {
		return {
		  code: 500,
		  message: '背单词状态查询失败',
		  error: err
		};
	}

};
